var ModalDialog;

window.__MODAL_SIMULATION_PLUGIN_INDEX = 0;

ModalDialog = (function () {
    function ModalDialog(options) {
        if (options == null) {
            options = {};
        }
        this.options = {
            closeBtn: "取消",
            confirmBtn: "确定",
            success: null,
            cancel: null,
            closeIcon: false
        };
        this.init(options);
    }

    ModalDialog.prototype.validateForm = function ($formEl) {
        return new Promise((resolve, reject) => {
            const rules = {'rules': {}, 'messages': {}};
            $formEl.find(".required-validate").each(function (e) {
                $el = $(this);
                $name = $el.attr('name');
                if (!rules[$name]) {
                    $type = $el.attr('type');
                    rules['rules'][$name] = {};
                    rules['rules'][$name]['required'] = true;
                    $commonmessage = $(this).attr('placeholder');
                    rules['messages'][$name] = {required: `${$commonmessage}`};
                    if ($el.attr('minlength')) {
                        rules['rules'][$name]['minlength'] = $el.attr('minlength');
                        rules['messages'][$name]['minlength'] = $el.attr('minmessage') || $commonmessage;
                    }
                    if ($el.attr('maxlength')) {
                        rules['rules'][$name]['maxlength'] = $el.attr('maxlength');
                        rules['messages'][$name]['maxlength'] = $el.attr('maxmessage') || $commonmessage;
                    }
                }
            });
            $formEl.validate({
                ...rules,
                submitHandler: function (form) {
                    resolve(form);
                    return false;
                }
            });
        })
    }
    ModalDialog.prototype.init = function (options) {
        $(".__MODAL_DIALOG_CONTAINER_CLASS__").remove();
        this.options = $.extend(this.options, options);
        window.__MODAL_SIMULATION_PLUGIN_INDEX++;
        this.dialogIndex = window.__MODAL_SIMULATION_PLUGIN_INDEX;
        $('body').append(this.render());
        this.el = $("#" + this.container);
        this.bindEvent();
        this.loading = false;
        return this;
    };

    ModalDialog.prototype.bindEvent = function () {
        var _this;
        _this = this;
        this.el.modal({backdrop: 'static', keyboard: false});

        if (this.el.find("form").length > 0) {
            this.validateForm(this.el.find("form")).then(function (form) {
                _this.success($(_this.el.find("form")[0]).serialize());
            });
        }
        this.el.on("click", ".modal-dialog-cancel", function (e) {
            return _this.close();
        }).on("click", ".modal-dialog-close", function (e) {
            return _this.close();
        }).on("click", ".modal-dialog-success", function (e) {
            console.log("ok");
            if (_this.el.find("form").length > 0) {
                return _this.el.find("form").submit();
            } else {
                return _this.success();
            }
        });
        return this;
    };

    ModalDialog.prototype.render = function () {
        this.container = "__MODAL_DIALOG_CONTAINER_" + this.dialogIndex + "__";
        return "<div id=\"" + this.container + "\" class=\"modal fade __MODAL_DIALOG_CONTAINER_CLASS__\" aria-hidden=\"true\">\n<div class=\"modal-dialog\">\n    <div class=\"modal-content\">\n        <div class=\"modal-body\">\n            " + this.options.content + "\n        </div>\n        <div class=\"modal-footer\">\n            " + (this.options.closeBtn !== false ? ("<button type='button' class='btn btn-white modal-dialog-cancel' data-dismiss='modal'>" + this.options.closeBtn + "</button>") : "") + "\n\n            " + (this.options.confirmBtn !== false ? ("<button type='button' class='btn btn-primary modal-dialog-success'>" + this.options.confirmBtn + "</button>") : "") + "\n        </div>\n    </div>\n</div>\n</div>";
    };

    ModalDialog.prototype.__loading = function () {
        if (this.loading) {
            this.el.addClass("sk-circle12 sk-circle");
            return this.el.modal("handleUpdate");
        }
    };

    ModalDialog.prototype.__loaed = function () {
        if (!this.loading) {
            this.el.removeClass("sk-circle12");
            return this.el.removeCLass("sk-circle");
        }
    };

    ModalDialog.prototype.show = function () {
        if (typeof this.options.beforeOpen === "function") {
            this.options.beforeOpen();
            this.el.modal("show");
        } else {
            this.el.modal("show");
        }
        return this;
    };

    ModalDialog.prototype.success = function () {
        var data, item, serialized, _i, _len;
        if (typeof this.options.success === "function") {
            console.log("options.successff");
            data = {};
            serialized = $("input,textarea,checkbox,select").serializeArray();
            for (_i = 0, _len = serialized.length; _i < _len; _i++) {
                item = serialized[_i];
                data[item.name] = item.value;
            }
            if (this.options.success(data, this)) {
                this.hide();
            }
        } else {
            this.hide();
        }
        return this;
    };

    ModalDialog.prototype.close = function () {
        if (typeof this.options.cancel === "function") {
            if (this.options.cancel()) {
                this.hide();
            }
        } else {
            this.hide();
        }
        return this;
    };

    ModalDialog.prototype.hide = function () {
        console.log("hide")
        return this.el.modal("hide");
    };

    return ModalDialog;

})();